package com.atguigu.controller;


import com.atguigu.entity.BaseBrand;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author zhangqiang
 * @since 2021-08-24
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController {
    @Value("${fastdfs.prefix}")
    private String fastdfsprefix;

    @Autowired
    BaseBrandService baseBrandService;
    //分页查询品牌信息
    @GetMapping("queryBrandByPage/{currentPageNum}/{pageSize}")
    public RetVal queryBrandByPage(@PathVariable Long currentPageNum,@PathVariable Long pageSize){
        Page<BaseBrand> baseBrands = new Page<>(currentPageNum, pageSize);
        QueryWrapper<BaseBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        baseBrandService.page(baseBrands, queryWrapper);
        return RetVal.ok(baseBrands);
    }

    //添加品牌信息
    @PostMapping
    public  RetVal saveBrand(@RequestBody BaseBrand baseBrand){
        baseBrandService.save(baseBrand);
        return RetVal.ok();
    }

    //3.根据id查询品牌信息
    @GetMapping("{brandId}")
    public RetVal saveBrand(@PathVariable Long brandId){
        BaseBrand byId = baseBrandService.getById(brandId);
        return RetVal.ok(byId);
    }

    //更新品牌信息
    @PutMapping
    public RetVal updateBrand(@RequestBody BaseBrand brand){
        baseBrandService.updateById(brand);
        return RetVal.ok();
    }

    //5.删除品牌信息
    @DeleteMapping("{brandId}")
    public RetVal remove(@PathVariable Long brandId){
        baseBrandService.removeById(brandId);
        return RetVal.ok();
    }

    //6.查询所有的品牌
    @GetMapping("getAllBrand")
    public RetVal getAllBrand(){
        List<BaseBrand> brandList = baseBrandService.list(null);
        return RetVal.ok(brandList);
    }
    //文件上传
    @PostMapping("/fileUpload")
    public RetVal fileUpload(MultipartFile file)throws Exception{
//        //需要一个配置文件告诉fastdfs在哪
//        String configFilePath = this.getClass().getResource("/tracker.conf").getFile();
//        //初始化
//        ClientGlobal.init(configFilePath);
//        //创建TrackerClient客户端
//        TrackerClient trackerClient = new TrackerClient();
//        //用TrackerClient获取连接
//        TrackerServer trackerServer = trackerClient.getConnection();
//        //创建StorageClient1
//        StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
//        //获取文件初始名称
//        String filename = file.getOriginalFilename();
//        //通过工具类获取后缀名
//        String extension = FilenameUtils.getExtension(filename);
//        //实现文件上传
//        String path = storageClient1.upload_appender_file1(filename.getBytes(), extension, null);
//        System.out.println("文件访问路径:"+fastdfsprefix+path);
//        return RetVal.ok(fastdfsprefix+path);
        //需要一个配置文件告诉fastdfs在哪里
        try {
            String configFilePath = this.getClass().getResource("/tracker.conf").getFile();
            //初始化
            ClientGlobal.init(configFilePath);
            //创建trackerClient 客户端
            TrackerClient trackerClient = new TrackerClient();
            //用trackerClient获取连接
            TrackerServer trackerServer = trackerClient.getConnection();
            //创建StorageClient1
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
            //文件原始名称 aaa.jpg
            String originalFilename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalFilename);
            //对文件实现上传
            String path = storageClient1.upload_appender_file1(file.getBytes(), extension, null);
            System.out.println("文件访问路径:"+fastdfsprefix+path);
            return RetVal.ok(fastdfsprefix+path);
        } catch (Exception e) {
            return RetVal.ok();
        }
    }


}

